package com.umakr.ax.common.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Maps;
import com.umakr.ax.common.enm.AttentionFrom;
import com.umakr.ax.common.mapper.ShowFabulousMapper;
import com.umakr.ax.common.model.RadioStationShow;
import com.umakr.ax.common.model.ShowFabulous;
import com.umakr.ax.core.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 秀点赞 服务实现类
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Service
public class ShowFabulousService extends BaseService<ShowFabulousMapper, ShowFabulous>{
    @Autowired
    RadioStationShowService radioStationShowService;
    @Autowired
    RadioStationFollowerService radioStationFollowerService;

    public Object fabulous(Long userId, Long rsId, Long showId, Integer type) {
        int errorCode = 0 ;
        String msg = "操作失败";
        Date date = new Date();
        Map<String,Object> data = Maps.newLinkedHashMap();
        //type == 1 表示点赞 type == 0 表示取消点赞
        if(type >= 0 && type  <= 1){
            //根据用户ID和电台秀ID 查找用户是否点赞过
            EntityWrapper<ShowFabulous> entityWrapper = new EntityWrapper<>();
            entityWrapper.eq("user_id",userId);
            entityWrapper.eq("show_id",showId);
            ShowFabulous showFabulous = selectOne(entityWrapper);
            //如果是点赞 新增记录 否则删除记录
            if(showFabulous == null){
                if(type == 0){
                    msg = "当前主播秀已删除，点赞失败";
                    errorCode = 0;
                }else {
                    showFabulous = new ShowFabulous();
                    showFabulous.setUserId(userId);
                    showFabulous.setRsId(rsId);
                    showFabulous.setShowId(showId);
                    showFabulous.setZanTime(date);
                    ShowFabulous record = update(showFabulous);
                    data.put("id",record.getId());
                    data.put("userId",record.getUserId());
                    data.put("rsId",record.getRsId());
                    data.put("showId",record.getShowId());
                    data.put("zanTime",record.getZanTime());
                    radioStationFollowerService.autoAttention(userId,rsId, AttentionFrom.FAVORITE);
                    msg = "点赞成功";
                    errorCode = 1;
                }
            }else if (showFabulous != null ){
                if(type == 1){
                    msg = "您已经点过赞了";
                    errorCode = 0;
                }else {
                    data.put("id",showFabulous.getId());
                    data.put("userId",showFabulous.getUserId());
                    data.put("rsId",showFabulous.getRsId());
                    data.put("showId",showFabulous.getShowId());
                    data.put("zanTime",showFabulous.getZanTime());
                    delete(showFabulous.getId());
                    msg = "取消成功";
                    errorCode = 1;
                }
            }else {
                msg = "操作失败";
            }
            //点赞或者取消点赞操作后需要更新数据
            EntityWrapper<ShowFabulous> showFabulousWrapper = new EntityWrapper<>();
            showFabulousWrapper.eq("show_id",showId);
            Integer fabulousCount = selectCount(showFabulousWrapper);

            RadioStationShow radioStationShow = radioStationShowService.queryById(showId);
            if(radioStationShow != null){
                radioStationShow.setLikeCount(fabulousCount);
                radioStationShowService.updateRadioStationShow(radioStationShow);
            }
        }
        return  resultMap(data,errorCode,msg);
    }
}
